home *** CD-ROM | disk | FTP | other *** search
/ The Wyoming CD-ROM - An Image Database 2.0 / Wyoming - An Image Database v2.0.iso / mac / NIH Image V1.57⁄68k / Macros / Plotting Macros < prev    next >
Text File  |  1995-01-23  |  14KB  |  582 lines

  1. macro 'Plot Histogram';
  2. var
  3.   max,scale:real;
  4.   i,margin,width,height:integer;
  5. begin
  6.   SaveState;
  7.   Margin:=10;
  8.   width:=256;
  9.   height:=0.6*256;
  10.   Measure;
  11.   SetForegroundColor(255);
  12.   SetBackgroundColor(0);
  13.   SetLineWidth(1);
  14.   SetNewSize(width+2*margin,height+2*margin);
  15.   MakeNewWindow('Histogram');
  16.   MakeRoi(margin,margin-1,width,height+1);
  17.   DrawBoundary;
  18.   max:=0;
  19.   for i:=1 to 254 do
  20.   if histogram[i]> max then max:=histogram[i];
  21.   scale:=height/max;
  22.   for i:=1 to 254 do begin
  23.     MakeRoi(margin+i,margin,1,histogram[i]*scale);
  24.     SetForegroundColor(i);
  25.     fill;
  26.  end;
  27.   SelectAll;
  28.   FlipVertical;
  29.   KillRoi;
  30.   RestoreState;
  31. end;
  32.  
  33. procedure DoColumnPlot(MaxCount: integer);
  34. {Plots the User1 column in the Results table.}
  35. var
  36.    xmin,xmax,ymin,ymax,i,xscale,yscale:real;
  37.    width,height,margin,pwidth,pheight:integer;
  38.    y,pbottom, barWidth, barLeft, barTop:integer;
  39.    sum:integer;
  40. begin
  41.   SaveState;
  42.   margin:=40;
  43.   width:=500;
  44.   height:=300;
  45.   sum:=0;
  46.   ymin:=0;
  47.   ymax:=-999999;
  48.   for i:=1 to maxCount do
  49.       if rUser1[i]>ymax then ymax:=rUser1[i];
  50.   xmin:=1;
  51.   xmax:=maxCount;
  52.   SetNewSize(width,height);
  53.   SetForeground(255);
  54.   SetBackground(0);
  55.   MakeNewWindow('Histogram');
  56.   pwidth:=width-2*margin;
  57.   pheight:=height-2*margin;
  58.   pbottom:=margin+pheight;
  59.   xscale:=pwidth/xmax;
  60.   yscale:=pheight/(ymax-ymin);
  61.   barWidth:=round(pwidth/maxCount)+1;
  62.   SetForeground(255);
  63.   SetBackground(0); 
  64.   SetLineWidth(1);
  65.   for i:=0 to maxCount-1 do begin
  66.      barLeft:=margin+i*xscale;
  67.      barTop:=pbottom-(rUser1[i+1]-ymin)*yscale;
  68.      MakeRoi(barLeft, barTop, barWidth, pBottom-barTop);
  69.      fill;
  70.      sum:=sum+(i+1)*rUser1[i+1];
  71.   end;
  72.   KillRoi;
  73.   MoveTo(margin,margin);
  74.   LineTo(margin,margin+pheight);
  75.   SetFont('Geneva');
  76.   SetFontSize(9);
  77.   SetText('Centered');
  78.   MoveTo(margin+4,margin+pheight+12);
  79.   writeln(xmin:1:2);
  80.   MoveTo(margin+pwidth,margin+pheight+12);
  81.   writeln(xmax:1:2);
  82.   SetText('Right Justified');
  83.   MoveTo(margin-2,margin+pheight-5);
  84.   writeln(ymin:1:2);
  85.   MoveTo(margin-2,margin);
  86.   writeln(ymax:1:2);
  87.   MoveTo(margin+pwidth/2-15, margin+pheight+12);
  88.   RestoreState;
  89.   ShowMessage('sum=',sum:1,'\ymax=',ymax:1);
  90. end;
  91.  
  92.  
  93. macro 'Plot Histogram using Bins';
  94. var
  95.   i, nBins, bin: integer;
  96.   ValuesPerBin: real;
  97.    n, mean, mode, min, max: integer;
  98. begin
  99.   nBins:=GetNumber('Number of Bins (1-256)', 10);
  100.   SetUser1Label('%');
  101.   Measure;
  102.   GetResults(n, mean, mode, min, max);
  103.   ValuesPerBin := 256 / nBins;
  104.   for bin := 1 to nBins do
  105.      rUser1[bin] := 0;
  106.   for i := 0 to 255 do begin
  107.      bin := trunc(i / ValuesPerBin) + 1;
  108.      rUser1[bin] := rUser1[bin] + Histogram[i];
  109.   end;
  110.    for bin := 1 to nBins do
  111.      rUser1[bin] :=  (rUser1[bin] / n) * 100.0;
  112.   SetCounter(nBins);
  113.   DoColumnPlot(nBins);
  114. end;
  115.  
  116. macro 'Plot XY Coordinates';
  117. {Plots the X-Y Coordinates of the current ROI.}
  118. var
  119.   i,w,h,width,height:integer;
  120.   xbase,ybase,RoiWidth,RoiHeight:integer
  121.   x,y,scale,xmax,ymax:real 
  122. begin
  123.   RequiresVersion(1.48);
  124.   if nCoordinates=0 then begin
  125.     PutMessage('No XY-Coordinates currently available.');
  126.     exit;
  127.   end;
  128.   GetRoi(xbase,ybase,RoiWidth,RoiHeight);
  129.   SaveState;
  130.   InvertY(false);
  131.   xmax:=0;
  132.   ymax:=0;
  133.   for i:=1 to nCoordinates do begin
  134.     x:=xCoordinates[i];
  135.     y:=yCoordinates[i];
  136.     if x>xmax then xmax:=x;
  137.     if y>ymax then ymax:=y;
  138.   end;
  139.   scale:=sqrt((300*300)/(xmax*ymax));
  140.   if (xmax*scale)>500 then scale:=500/xmax;
  141.   if (ymax*scale)>500 then scale:=500/ymax;
  142.   SetForegroundColor(255);
  143.   SetBackgroundColor(0);
  144.   SetNewSize(xmax*scale+20,ymax*scale+20);
  145.   MakeNewWindow('Outline');
  146.   MoveTo(xCoordinates[1]*scale+10,yCoordinates[1]*scale+10);
  147.   for i:=2 to nCoordinates do
  148.     LineTo(xCoordinates[i]*scale+10,yCoordinates[i]*scale+10);
  149.   SetFont('Helvetica');
  150.   SetFontSize(12);
  151.   SetText('No background, Left Justified');
  152.   GetPicSize(width,height);
  153.   MoveTo(width/3,height/3);
  154.   Writeln(nCoordinates:1,' coordinate pairs');
  155.   Writeln('Origin=',xbase:1,', ',ybase:1);
  156.   Writeln('xmax=',xmax:1, ', ymax=',ymax:1,);
  157.    RestoreState;
  158. end;
  159.  
  160.  
  161. procedure PlotProfile2(integrate:boolean);
  162. var
  163.   xmin,xmax,ymin,ymax,i,xscale,yscale:real;
  164.   width,height,margin,pwidth,pheight:integer;
  165.   count:integer;
  166.   ppv:integer; {Pixels per Value}
  167. begin
  168.   SaveState;
  169.   margin:=40;
  170.   width:=500;
  171.   height:=300;
  172.   GetPlotData(count,ppv,ymin,ymax);
  173.   if count=0 then begin
  174.     PutMessage('No plot data available.');
  175.     exit;
  176.   end;
  177.   if integrate then begin
  178.      ymin:=ymin*ppv;
  179.      ymax:=ymax*ppv;
  180.   end;
  181.   xmin:=0;
  182.   xmax:=count-1;
  183.   SetNewSize(width,height);
  184.   SetForeground(255);
  185.   SetBackground(0);
  186.   MakeNewWindow('Plot');
  187.   pwidth:=width-2*margin;
  188.   pheight:=height-2*margin;
  189.   xscale:=pwidth/(xmax-xmin);
  190.   yscale:=pheight/(ymax-ymin);
  191.   SetForeground(255);
  192.   SetBackground(0); 
  193.   SetLineWidth(1); 
  194.   MoveTo(margin,margin);
  195.   if integrate then for i:=0 to count-1 do
  196.        LineTo(margin+i*xscale,margin+(PlotData[i]*ppv-ymin)*yscale)
  197.   else  for i:=0 to count-1 do
  198.         LineTo(margin+i*xscale,margin+(PlotData[i]-ymin)*yscale);
  199.   MakeRoi(margin,margin,pwidth+1,pheight+2);
  200.   MoveTo(margin,margin);
  201.   LineTo(margin+pwidth,margin);
  202.   MoveTo(margin,margin);
  203.   LineTo(margin,margin+pheight);
  204.   FlipVertical;
  205.   KillRoi;
  206.   SetFont('Geneva');
  207.   SetFontSize(9);
  208.   SetText('Centered');
  209.   MoveTo(margin+4,margin+pheight+12);
  210.   writeln(xmin:1:2);
  211.   MoveTo(margin+pwidth,margin+pheight+12);
  212.   writeln(xmax:1:2);
  213.   SetText('Right Justified');
  214.   MoveTo(margin-2,margin+pheight-5);
  215.   writeln(ymin:1:2);
  216.   MoveTo(margin-2,margin);
  217.   writeln(ymax:1:2);
  218.   RestoreState;
  219. end;
  220.  
  221.  
  222. macro 'Plot Profile';
  223. begin
  224.   PlotProfile2(false);
  225. end;
  226.  
  227. macro 'Plot Integrated Profile';
  228. begin
  229.   PlotProfile2(true);
  230. end;
  231.  
  232. macro 'Plot Radial Profiles [R]';
  233. var
  234.   x1,y1,x2,y2,pi,angle,delta:real;
  235.   LineWidth,i,nLines,radius,PlotWidth,PlotHeight:integer;
  236.   MinPlotWidth,hMargin,vMargin,PlotLeft,PlotTop:integer;
  237.   LeftMargin,RightMargin,TopMargin,BottomMargin:integer;
  238.   ImageWindow,PlotWindow:integer;
  239.   nPixels,mean,mode,min,max:real;
  240. begin
  241.   RequiresVersion(1.54);
  242.   SaveState;
  243.   GetLine(x1,y1,x2,y2,LineWidth);
  244.   if x1<0 then begin
  245.     PutMessage('Please select a point by clicking with the line tool.');
  246.     exit;
  247.   end;
  248.   radius:=GetNumber('Radius:',20);
  249.   nLines:=GetNumber('Number of Lines:',8);
  250.   MinPlotWidth:=140;
  251.   pi:=3.14159;
  252.   delta:=2.0*pi/nLines;
  253.   angle:=0.0;
  254.   PlotWidth:=radius;
  255.   if PlotWidth<MinPlotWidth then PlotWidth:=MinPlotWidth;
  256.   PlotHeight:=0.4*PlotWidth;
  257.   SetPlotSize(PlotWidth,PlotHeight);
  258.   MakeOvalRoi(x1-radius,y1-radius,radius*2,radius*2);
  259.   Measure;
  260.   GetResults(nPixels,mean,mode,min,max);
  261.   min:=min-10;
  262.   if min<0 then min:=0;
  263.   max:=max+10;
  264.   if max>255 then max:=255;
  265.   SetPlotScale(cValue(min),cValue(max));
  266.   SetPlotLabels(false);
  267.   hMargin:=5;
  268.   vMargin:=5;
  269.   LeftMargin:=38;
  270.   TopMargin:=10;
  271.   RightMargin:=20;
  272.   BottomMargin:=20;
  273.   PlotLeft:=hMargin-LeftMargin;
  274.   PlotTop:=vMargin-TopMargin;
  275.   SetNewSize(PlotWidth+2*hMargin,PlotHeight*nLines);
  276.   SetForegroundColor(255);
  277.   SetBackgroundColor(0);
  278.   ImageWindow:=PicNumber;
  279.   MakeNewWindow('Plots');
  280.   PlotWindow:=PicNumber;
  281.   SelectPic(ImageWindow);
  282.   for i:=1 TO nLines do begin
  283.     x2:=x1+round(radius*cos(angle));
  284.     y2:=y1+round(radius*sin(angle));
  285.     MakeLineRoi(x1,y1,x2,y2);
  286.     PlotProfile;
  287.     Copy;
  288.     SelectPic(PlotWindow);
  289.     MakeRoi(PlotLeft,PlotTop,PlotWidth+LeftMargin+RightMargin,
  290.           PlotHeight+TopMargin+BottomMargin);
  291.     Paste;
  292.     DoOr;
  293.     PlotTop:=PlotTop+PlotHeight-1;
  294.     SelectPic(ImageWindow);
  295.     angle:=angle+delta;
  296.   end;
  297.   RestoreState;
  298. end;
  299.  
  300.  
  301. macro 'Circular Profile Plot [C]';
  302. var
  303.   radius,pi,angle,dx,dy,delta:real;
  304.   x1,y1,x2,y2:real;
  305.   npoints,i,value,LineWidth,x,y,px:integer;
  306. begin
  307.   GetLine(x1,y1,x2,y2,LineWidth)
  308.   if x1<0 then begin
  309.     PutMessage('Please select a point by clicking with the line tool.');
  310.     exit;
  311.   end;
  312.   x:=x1+(x2-x1)/2;
  313.   y:=y1+(y2-y1)/2;
  314.   radius:=sqrt(sqr(x2-x1)+sqr(y2-y1))/2;
  315.   if radius<3 then begin
  316.     PutMessage('The line selection must be longer than 5 pixels.');
  317.     exit;
  318.   end;
  319.   npoints:=radius*2;
  320.   pi:=3.14159;
  321.   delta:=2.0*pi/npoints;
  322.   angle:=0.0;
  323.   px:=0;
  324.   for i:=1 TO npoints do begin
  325.     dx:=round(radius*cos(angle));
  326.     dy:=round(radius*sin(angle));
  327.     value:=GetPixel(x+dx,y+dy);
  328.     PutPixel(x+dx,y+dy,255);
  329.     PutPixel(px,0,value);
  330.     px:=px+1;
  331.     angle:=angle+delta;
  332.   end;
  333.   MakeLineRoi(0,0,npoints,0);
  334.   PlotProfile;
  335.   KillRoi;
  336. end;
  337.  
  338. macro 'Export Profile Plots…';
  339. var
  340.   y,yInc,width,height,n:integer;
  341. begin
  342.   yInc:=GetNumber('Y Increment:',10);
  343.   GetPicSize(width,height);
  344.   y:=0;
  345.   n:=0;
  346.   SetExport('Plot Values');
  347.   repeat
  348.     MakeLineRoi(0,y,width-1,y);
  349.     PlotProfile;
  350.     Export('PLOT',n:4);
  351.     n:=n+1;
  352.     y:=y+yInc;
  353.   until y>=height;
  354. end;
  355.  
  356.  
  357. procedure PlotMeans;
  358. {Plots the mean column in the Results table.}
  359. var
  360.    xmin,xmax,ymin,ymax,i,xscale,yscale:real;
  361.   width,height,margin,pwidth,pheight:integer;
  362.   y,pbottom:integer;
  363. begin
  364.   margin:=40;
  365.   width:=500;
  366.   height:=300;
  367.   ymax:=-999999;
  368.   ymin:=999999;
  369.   for i:=1 to rCount do begin
  370.     y:=rMean[i];
  371.     if y>ymax then ymax:=y;
  372.     if y<ymin then ymin:=y;
  373.   end;
  374.   xmin:=0;
  375.   xmax:=rCount-1;
  376.   SetNewSize(width,height);
  377.   SetForeground(255);
  378.   SetBackground(0);
  379.   MakeNewWindow('Z-Axis Profile Plot');
  380.   pwidth:=width-2*margin;
  381.   pheight:=height-2*margin;
  382.   pbottom:=margin+pheight;
  383.   xscale:=pwidth/(xmax-xmin);
  384.   yscale:=pheight/(ymax-ymin);
  385.   SetForeground(255);
  386.   SetBackground(0); 
  387.   SetLineWidth(1);
  388.   MoveTo(margin,pbottom-(rMean[1]-ymin)*yscale);
  389.   for i:=2 to rCount do
  390.      LineTo(margin+(i-1)*xscale,pbottom-(rMean[i]-ymin)*yscale);
  391.   MoveTo(margin,pbottom);
  392.   LineTo(margin+pwidth,pbottom);
  393.   MoveTo(margin,margin);
  394.   LineTo(margin,margin+pheight);
  395.   SetFont('Geneva');
  396.   SetFontSize(9);
  397.   SetText('Centered');
  398.   MoveTo(margin+4,margin+pheight+12);
  399.   writeln(xmin:1:2);
  400.   MoveTo(margin+pwidth,margin+pheight+12);
  401.   writeln(xmax:1:2);
  402.   SetText('Right Justified');
  403.   MoveTo(margin-2,margin+pheight-5);
  404.   writeln(ymin:1:2);
  405.   MoveTo(margin-2,margin);
  406.   writeln(ymax:1:2);
  407. end;
  408.  
  409. macro 'Plot Z-Axis Profile [Z]';
  410. {Plots the average density of an roi through a stack.}
  411. var
  412.   left,top,width,height,i:integer;
  413. begin
  414.   if (nPics=0) or (nSlices=0) then begin
  415.      PutMessage('This macro requires a stack.');
  416.      exit;
  417.   end;
  418.   GetRoi(left,top,width,height);
  419.   if width=0 then begin
  420.      PutMessage('Selection required.');
  421.      exit;
  422.   end;
  423.   ResetCounter;
  424.   {SetOptions('Mean');}
  425.   for i:= 1 to nSlices do begin
  426.      SelectSlice(i);
  427.      Measure;
  428.   end;
  429.   PlotMeans;
  430.  end;
  431.  
  432.  
  433. macro 'Plot XYZ';
  434. {
  435. Plots X-Y coordinate points with an optional intensity(Z). Values are read from
  436. a 2 or 3 column tab-delimited text file. Data must be scaled as follows:
  437. 0<=X<width; 0<=Y<height; 0<=Z<=255.
  438. }
  439. var
  440.   width,height:integer;
  441. begin
  442.   SaveState;
  443.   width:=500;
  444.   height:=500;
  445.   SetNewSize(width,height);
  446.   SetForeground(255);
  447.   SetBackground(0);
  448.   MakeNewWindow('Plot');
  449.   PlotXYZ;
  450.   RestoreState;
  451. end;
  452.  
  453. macro 'Draw Fitted Ellipse in White';
  454. var
  455.   left,top,width,height:real;
  456. begin
  457.   GetRoi(left,top,width,height);
  458.   if width=0 then begin
  459.     PutMessage('This macro requires a selection.');
  460.     exit;
  461.   end;
  462.   SetOptions('Area; Mean; X-Y Center');
  463.   Measure;
  464.   SetOption; MarkSelection;
  465.   KillRoi;
  466.   SelectAll;
  467.   KillRoi;
  468.  end;
  469.  
  470. macro 'Draw Calibration Bar [B]';
  471. {Generates a vertical calibration bar with labels.}
  472. var
  473.   top,left,width,height,nLabels,i:integer;
  474.   vloc,fwidth,digits,value:integer;
  475. begin
  476.   SaveState;
  477.   GetRoi(left,top,width,height);
  478.   if width=0 then begin
  479.     PutMessage('This macro requires a selection.');
  480.     exit;
  481.   end;
  482.   if width>height then begin
  483.     PutMessage('Selection must be vertically oriented.');
  484.     exit;
  485.   end;
  486.   nLabels:=round(height/25);
  487.   if nLabels<2 then nLabels:=2;
  488.   SetFontSize(9);
  489.   SetFont('Monaco');
  490.   SetText('Left Justified, With Background');
  491.   DrawScale;
  492.   {FlipVertical;}
  493.   KillRoi;
  494.   SetForeground(255); {black}
  495.   SetBackground(0); {white}
  496.   if calibrated then begin
  497.      fwidth:=7;
  498.     digits:=4;
  499.   end else begin
  500.     fwidth:=3;
  501.     digits:=0;
  502.   end;
  503.   vloc:=top;
  504.   for i:=0 to nLabels-1 do begin
  505.     vloc:=top+round(i*((height-1)/(nLabels-1)));
  506.     if vloc>=(top+height) then vloc:=top+height-1;
  507.     MoveTo(left+width+4,vloc+3);
  508.     value:=cvalue(GetPixel(left,vloc));
  509.     Write(value:fwidth:digits);
  510.     vloc:=vloc+round(height/(nLabels-1));
  511.   end;
  512.   RestoreRoi;
  513.   SetForeground(0); {white}
  514.   InsetRoi(-1);
  515.   DrawBoundary;
  516.   KillRoi;
  517.   RestoreState;
  518. end;
  519.  
  520.  
  521. macro 'Show Polar Coordiates [P]';
  522. {Returns polar coordinates of a point selected with the mouse, using centre 
  523. of the image as 0,0.  Data are displayed in the Info window as distance from 
  524. centre of image, and angle in degrees measured clockwise, where 0 is the 
  525. 12 o'clock position}
  526. var
  527.     Wide, High,x2,y2:integer;
  528.     x1,y1,D,Theta,rad:real;
  529. begin
  530.    rad:=180/3.14159265;
  531.    InvertY(true);
  532.    GetPicSize(Wide,High);
  533.    SetCursor('cross');
  534.    repeat
  535.       GetMouse(x2,y2);
  536.       x1:=Wide/2;
  537.       y1:=High/2;
  538.       y2:=High-y2
  539.       if (x1=x2) and (y1=y2) then begin
  540.          D:=0;
  541.          Theta:=0;
  542.       end;
  543.       if (y1<>y2) then begin
  544.          D:= sqrt((sqr(x2-x1))+ (sqr(y2-y1)));
  545.          Theta:=rad*(arctan((x2-x1)/(y2-y1)));
  546.       end;
  547.       if (y2<y1) then begin
  548.          Theta:=180 + Theta; 
  549.       end;
  550.       if (x2<x1) and (y2>y1) then begin
  551.          Theta:=360+Theta;
  552.       end;
  553.       ShowMessage('Distance: ',D:5:1'\''Angle: ',Theta:5:1); 
  554.       Wait(0.2);
  555.    until button;
  556. end;
  557.  
  558.  
  559. macro 'Record XY [X]';
  560. {Records the X-Y Coordinates of each pixel in the perimeter
  561. of a particle (selected with the wand) and saves the data to a
  562. comma-delimited text file}
  563. var
  564.     i,w,h:real;
  565.    xbase,ybase,width,height,RoiWidth,RoiHeight:real
  566.    x,y,xmax,ymax:real 
  567. begin
  568.    GetPicSize(width,height);
  569.    GetRoi(xbase,ybase,RoiWidth,RoiHeight);
  570.     if (RoiWidth=0) or (nCoordinates=0) then begin
  571.       PutMessage('Select a particle with the wand.');
  572.        exit;
  573.    end;
  574.   InvertY(false);
  575.    NewTextWindow('XY Data',150,400);
  576.    for i:=1 to nCoordinates do
  577.       Writeln(i,',',xCoordinates[i]+xbase:5:0,',',Height-yCoordinates[i]-ybase:5:0);
  578. end;
  579.  
  580.  
  581.  
  582.